<?php
use yii\helpers\Html;
use yii\helpers\Url;
use backend\widgets\GridView;
use yii\widgets\Breadcrumbs;
use yii\bootstrap\Modal;
use backend\models\service\AdminService as Admin;

use backend\models\Admin as Admin_b;

Modal::begin([
    'id' => 'common-modal',
    'header' => '<h4 class="modal-title"></h4>',
    'footer' => '<a href="#" class="btn btn-primary" data-dismiss="modal">Close</a>',
]);


$js = <<<JS
$(".modaldialog").click(function(){ 
        aUrl = $(this).attr('data-url');
        aTitle = $(this).attr('data-title');
        
        $($(this).attr('data-target')+" .modal-title").text(aTitle);
        $($(this).attr('data-target')).modal("show")
             .find(".modal-body")
             .load(aUrl); 
        return false;
   }); 
JS;
$this->registerJs($js);

Modal::end(); 
$this->title = '渠道列表';
$this->params['breadcrumbs'][] = $this->title;
?>

<div class="RightCenter clear">
    <div class="RightTime">
        <div class="Time">
            <h3>渠道列表</h3>
        </div>
    </div>
    <p style="margin:30px;"><?php echo Html::a('新建渠道', 'create', [
            'id' => 'create-channel',
            'class' => 'modaldialog btn btn-success',
            'data-toggle' => 'modal',
            'data-url' => Url::toRoute(['create-channel']),
            'data-title' => '新建渠道',
            'data-target' => '#common-modal',
        ]);?>
    </p>
    <div class="Read_date" style="margin-top:20px;margin-bottom: 5px;height:60px;">
        <ul>
            <li class="Read_LisJ" style="margin-top:12.5px;">
                <div class="Seek">
                    <input type="text" class="form-control AppIN" id="exampleInputAmount" placeholder="搜索渠道名称" value="<?php echo isset($search['name']) && $search['name'] ? trim($search['name']) : '';?>">
                    <button type="button" class="btn btn-primary SS">搜索</button>
                    <button type="button" class="btn btn-primary CZ">重置</button>
                </div>
            </li>
        </ul>
    </div>
    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'columns' => [
            
            [
                'format' => 'raw',
                'label' => '合作方信息',
                'headerOptions' => ['width' => 80],
                'value' => function($data) {
                    return $data->username . '</br>' . $data->admin_sn ;
                }
            ],
            [
                'format' => 'raw',
                'label' => '渠道信息',
                'headerOptions' => ['width' => 80],
                'value' => function($data){
                    return $data->username. '</br>' .$data->id;
                }
            ],
            [
                'label' => '推文数量',
                'headerOptions' => ['width' => 20],
                'format' => 'raw',
                'value' => function($data){
                $res = Admin_b::getTweetCount($data->id);
                    return '<a href="/admin/channel-info?id='.$data->id.'">'.$res['cnt'].'</a>';
                }
            ],
            [
                'label' => '充值总额（元）',
                'headerOptions' => ['width' => 50],
                'value' => function($data){
                    $res = Admin_b::getChargeSum($data->id);
                    return $res['money'];
                }
            ],
            [
                'label' => '充值笔数(次)',
                'headerOptions' => ['width' => 20],
                'value' => function($data){
                    $res = Admin_b::getChargeSum($data->id);
                    return $res['cnt'];
                }
            ],
            [
                'label' => '收入(阅币)',
                'headerOptions' => ['width' => 50],
                'value' => function($data){
                    $res = Admin_b::getConsumeSum($data->id);
                    return $res['money'];
                }
            ],
            [
                'label' => '阅币笔数',
                'headerOptions' => ['width' => 20],
                'value' => function($data){
                    $res = Admin_b::getConsumeSum($data->id);
                    return $res['cnt'];
                }
            ],
            [
                'attribute' => 'cost',
                'label' => '成本(元)',
                'headerOptions' => ['width' => 50],
            ],
            [
                'label' => '回报率',
                'headerOptions' => ['width' => 50],
            ],
            
            [
                'attribute' => 'remark',
                'label' => '备注',
                'headerOptions' => ['width' => 100],
            ],
            [
                'class' => \yii\grid\ActionColumn::className(),
                'header' => '操作',
                'headerOptions'=>['width' => 180],
                'contentOptions'=>['class'=>'action-link'],
                'template' => '{channel-info} {update-channel} {del-channel}',
                'buttons' => [
                    'channel-info' => function ($url, $model, $key) {
                        return Html::a('详细信息', $url)." ";
                    },
                   'update-channel' =>  function ($url, $model, $key) {
                        return Html::a('编辑', $url, [
                            'id' => 'update-channel',
                            'class' => 'modaldialog',
                            'data-toggle' => 'modal',
                            'data-url' => Url::toRoute($url),
                            'data-title' => '编辑',
                            'data-target' => '#common-modal',
                        ])." ";
                    },
                   'del-channel' => function ($url, $model, $key) {
                        return Html::a('删除', $url, [
                            'data' => [
                                'confirm' => '确定/删除？',
                                'method' => 'post',
                            ],
                        ]);
                    },
                    
                ],
            ],
        ],
    ]); ?>
</div>
<script>
    $(".SS").click(function(){
        var name = $("#exampleInputAmount").val();
        if(name != ''){
            location.href="/admin/channel-lists?name="+name
        }
    });
    
    $(".CZ").click(function(){
        location.href="/admin/channel-lists"
    });
</script>